package jdbc.homework02;

import java.sql.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class StudentDaoTest {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根

		// 1.查询学生信息
		// StudentDao sdt=new StudentDao();
		Student s = null;
		List<Student> list = null;
		list = StudentDao.findAll();
		for (Student st : list) {
			System.out.println(st.getStuno() + "\t" + st.getSname() + "\t" + st.getSex() + "\t" + st.getTel() + "\t"
					+ st.getQq() + "\t" + st.getWechat() + "\t" + st.getGradeid() + "\t" + st.getEnterdate() + "\t"
					+ st.getFromcity() + "\t" + st.getBrithday() + "\t" + st.getAddress() + "\t" + st.getPass());
		}

		// 2.增加学生
		s = new Student();
		s.setSname("胡歌");
		s.setSex("男");
		s.setTel("123456789");
		s.setQq("12346789");
		s.setWechat("12356789");
		s.setEnterdate(Date.valueOf("2019-08-02"));
		s.setBrithday(Date.valueOf("1994-01-02"));
		s.setFromcity("合肥");
		s.setAddress("合肥");
		s.setGradeid(1);
		s.setPass("1234567");

		int r = StudentDao.addStudent(s);
		if (r == 0) {
			System.out.println("增加失败");
		} else {
			System.out.println("增加成功");
		}

		// 3.修改学生信息
		s = new Student();
		s.setSname("华为成");
		s.setSex("女");
		s.setStuno(220);

		int a = StudentDao.update(s);
		if (a == 0) {
			System.out.println("修改失败");
		} else {
			System.out.println("修改成功");
		}

		// 4.删除学生
		s = new Student(221);
		int b = StudentDao.delete(s);
		if (b == 0) {
			System.out.println("删除失败");
		} else {
			System.out.println("删除成功");
		}

		// 5.分页查询
		Scanner in = new Scanner(System.in);
		System.out.println("请输入查询第几页：");
		int pageNo = in.nextInt();
		System.out.println("请输入每页多少条信息：");
		int pagesize = in.nextInt();
		list = StudentDao.limiteSelect(pageNo, pagesize);

		for (Student st : list) {
			System.out.println(st);
		}
		if (list.size() == 0) {
			System.out.println("该页没有信息");
		}

		// 6.分组查询
		Map<String, String> map = StudentDao.groupBy();
		Set n = (Set) map.keySet();

		Iterator il = n.iterator();
		while (il.hasNext()) {
			Object o = il.next();
			System.out.println(o + " " + map.get(o));
		}

		// 统计人数
		int num = StudentDao.count();
		System.out.println("总人数为：" + num);

	}

}
